/**
 * @title:<h3> 网络请求工具js <h3>
 * @author: loy
 * @date: 2018-10-10 15:13
 * @params
 * @return
 **/
var requestUtilParams = {
    pageSize: 10
    , host: "http://localhost:8031/ssmService/"
    //,host:"http://localhost:63342/loy/"
    ,staticJson:false//加载静态json数据
    ,token:""//防止重复提交id
    ,ajaxSetting:{
        type:"post",
        async:true
    }
    /**
     * @title:<h3>  公共的ajax请求<h3>
     * @author: loy
     * @date: 2018-10-18 15:42
     * @params
     * @return
     **/
    , xhr: function (actionName, data,setting) {
        if (setting==null){
            setting=requestUtilParams.ajaxSetting;
        };
        $.each(requestUtilParams.ajaxSetting,function (k,v) {
            if (setting[k]==null){
                setting[k]=v;
            };
        });
        requestUtilParams.getToken();//获得token
        if (data == "" || data == null) {
            data = "token=" + requestUtilParams.token;
        } else if (typeof data == "string") {
            data += "&token=" + requestUtilParams.token;
        } else if (typeof data == "object") {
            data["token"] = requestUtilParams.token;
        }
        var sendUrl = actionName;
        if(requestUtilParams.staticJson){
            //说明加载静态json数据
            sendUrl = "files/data/"+actionName+".json";
        }
        var xhrReq = $.ajax({
            url: requestUtilParams.host + sendUrl,
            type: setting.type,
            dataType: "json",
            async:setting.async,
            data: data,
            success: function (res) {

            }, error: function (res,status,xr) {
                requestUtilParams.ajaxError(res,status,xr);
            },
            complete:function (res) {
                requestUtilParams.getToken(1);
            }

        });
        return xhrReq;
    },
    /**
     * @title:<h3> 单个删除与批量删除数据 <h3>
     * @author: loy
     * @date: 2018/10/26 14:21
     * @params
     * @return
     **/
    deleteData:function (actionName,pk,tableNode,callbackMethod) {
        if(pk==""||pk==null){//如果pk为空则为批量删除
            var ids = "";
            $(tableNode+" tbody input.inpDel:checked").each(function () {
                ids +=","+$(this).val();
            });
            if(ids.length>0){
                pk=ids.substr(1);
            }else {
                alert("请选择要删除的数据");
                return false;
            }
        }
        if(confirm("确认删除数据吗?")){
            requestUtilParams.xhrGet(actionName+pk).done(function (rs) {
                alert(rs.message);
                callbackMethod($("#printPage .layui-laypage-curr").text());
            })
        }
    }
    /**
     * @title:<h3> 分页查询公共方法 <h3>
     * @author: loy
     * @date: 2018-10-18 9:15
     * @params
     * @return
     **/
    , findPageData: function (actionName, cpage, tableNode, printPageNode, jumpMethod) {
        if (cpage == null) {
            cpage = 1;
        }
        var data = "baseModel.queryParams.curr_page=" + cpage + "&baseModel.queryParams.page_size=" + requestUtilParams.pageSize;
        var xhr=requestUtilParams.xhr(actionName, data).done(function (res) {
            //alert("请求成功")
            var list = res.data.list;
            var $tbody = $(tableNode + " tbody");
            $(tableNode+" thead th>input[type='checkbox']").prop("checked",false);
            $tbody.empty();
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: printPageNode //注意，这里的 test1 是 ID，不用加 # 号
                    , count: res.data.total //数据总数，从服务端得到
                    , curr: res.data.prePage + 1//当前页
                    , limit: res.data.pageSize//每页显示的记录数
                    , jump: function (obj, first) {//回调函数
                        //首次不执行
                        if (!first) {
                            //do something
                            jumpMethod(obj.curr);
                        }
                    }
                });
            });
        });

        return xhr;

    },
    getToken:function(isPush){
        if(isPush==1){
            requestUtilParams.token = new Date().getTime();
        }else if (requestUtilParams.token==null||requestUtilParams.token==""){
            requestUtilParams.token=new Date().getTime();
        }
        return requestUtilParams.token;
    },
    /*
    * @title:<h3> xhrGet方法  <h3>
    * @author: loy
    * @date: 2018/10/23 14:16
    * @params
    * @return
    **/
    xhrGet:function(actionName,setting){
        if(setting==null){
            setting={};
        }
        setting.type="get";
        return  this.xhr(actionName,"",setting);
    },
    /*
* @title:<h3> 文件上传 <h3>
* @author: loy
* @date: 2018/10/24 18:18
* @params
* @return
**/
    xhrUpload:function (actionName,formId) {
    var data = new FormData($(formId)[0]);
    $(formId).attr("enctype","multipart/form-data");
    data.append("token",requestUtilParams.getToken());
    var xhrReq = $.ajax({
        url: requestUtilParams.host+actionName,
        type: "post",
        contentType:false,
        processData:false,
        data: data,
        dataType:"json",
        success: function (res) {

        }, error: function (res,status,xr) {
            requestUtilParams.ajaxError(res,status,xr);

        },
        complete:function (res) {
            requestUtilParams.getToken(1);
        }

    });
    return xhrReq;
},
    /*
    * @title:<h3> ajax异常处理 <h3>
    * @author: loy
    * @date: 2018/10/24 18:20
    * @params
    * @return
    **/
    ajaxError:function(res,status,xr){
        if(res.responseJSON!=null){
            console.log("请求失败：" + res.responseJSON.message);
            alert( res.responseJSON.message)
        }else{
            alert( "请求失败");
        }
    },
    /**
     * @title:<h3>创建高级查询对话框  <h3>
     * @author: loy
     * @date: 2018-10-10 15:16
     * @params 【model:查询参数模型,dialogId：对话框id】
     * @return
     **/
    createAdvancedQueryDialog: function (model, dialogId) {
        if (dialogId == null) {
            dialogId = "#dialogAdvancedQuery_" + new Date().getTime();//设置高级查询对话框唯一id
        }
        if ($(dialogId).length > 0) {
            layui.use(["layer"], function () {
                var layer = layui.layer;
                var index = layer.open({
                    type: 1,
                    content: $(dialogId)
                });
                layer.style(index, {
                    width: '700px',
                });
            });
            return;
        }
        //如果不存在对话框,则创建对话框
        $("body").append('<div class="auxiliaryGroup hidden"><div class="layui-tab dialogAdvancedQuery" id="' + dialogId.substr(1) + '">\n' +
            '  <ul class="layui-tab-title">\n' +
            '    <li class="layui-this">快速查询</li>\n' +
            '    <li>高级查询</li>\n' +
            '  </ul>\n' +
            '  <div class="layui-tab-content">\n' +
            '    <div class="layui-tab-item layui-show"><form class="layui-form formQueryGroup"><div class="queryItemGroup layui-form-item"></div>' +
            '  <div class="layui-form-item">\n' +
            '    <div class="layui-input-block">\n' +
            '      <button class="layui-btn" >搜索</button>\n' +
            '      <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
            '    </div>\n' +
            '  </div></form></div>\n' +
            '    <div class="layui-tab-item"><form class="layui-form formAdvancedQuery"><div class="advancedQueryItemGroup layui-form-item"></div>' +
            '  <div class="layui-form-item">\n' +
            '    <div class="layui-input-block">\n' +
            '      <button class="layui-btn btnAddQueryItem" type="button">追加条件</button>\n' +
            '      <button class="layui-btn">搜索</button>\n' +
            '      <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
            '    </div>\n' +
            '  </div></form></div>\n' +
            '  </div>\n' +
            '</div></div>');
        var $queryGroup = $(dialogId + " .queryItemGroup");
        //添加快速查询项
        $.each(model, function (k, v) {
            var $item = $("<div class='layui-col-xs6 layui-col-sm6 layui-col-md6'><label class='layui-form-label'>" + v.comment + "</label><div class='layui-input-inline inputGroup'></div></div>");
            if (v.item != null) {//如果是下拉选项
                var $select = $("<select name='" + k + "'><option value=''>不限</option></select>");
                $.each(v.item, function (kk, vv) {
                    $select.append("<option value='" + kk + "'>" + vv + "</option>");
                });
                $item.find(".inputGroup").append($select);
            } else if (v.type == "date") {//如果是日期
                $item.find(".inputGroup").append(" <input class='layui-input searchItemDate' readonly type='text' name='" + k + "'>");
            } else if (v.type == "datetime") {
                $item.find(".inputGroup").append(" <input class='layui-input searchItemDatetime' readonly type='text' name='" + k + "'>");
            } else {
                $item.find(".inputGroup").append(" <input class='layui-input' type='text' name='" + k + "'>");
            }
            $queryGroup.append($item);
        });
        $queryGroup.append("<div class='layui-col-xs6 layui-col-sm6 layui-col-md6'><label class='layui-form-label'>分页大小</label><div class='layui-input-inline'><input class='layui-input page_size' type='text' name='page_size'></div></div>");
        $queryGroup.append($queryGroup);//添加快速查询搜索内容
        //添加高级查询项
        var $advancedQueryGroup = $(dialogId + " .advancedQueryItemGroup");//高级查询条件容器
        //定义添加高级查询项方法
        function addAdvancedItem() {
            var $item = $("<div class='layui-form-item'></div>");
            //添加下拉框（且、或）
            $item.append("<div class='layui-inline logicalGroup'><select class='logical'><option>且</option><option>或</option></select></div>");
            //添加下拉框字段选择
            var $selectField = $("<select class='field'></select>");
            $.each(model, function (k, v) {
                $selectField.append("<option value='" + k + "'>" + v.comment + "</option>");
            });
            $item.append($("<div class='layui-inline fieldGroup'></div>").append($selectField));
            //添加下拉菜单关系运算符
            var $selectRelational = $("<select class='relational'></select>");
            $selectRelational.append("<option value=''>包含</option>");
            $selectRelational.append("<option value=''>以...开始</option>");
            $selectRelational.append("<option value=''>以...结尾</option>");
            $selectRelational.append("<option value=''>等于</option>");
            $selectRelational.append("<option value=''>大于</option>");
            $selectRelational.append("<option value=''>大于等于</option>");
            $selectRelational.append("<option value=''>小于</option>");
            $selectRelational.append("<option value=''>小于等于</option>");
            $selectRelational.append("<option value=''>不等于</option>");
            $selectRelational.append("<option value=''>IN</option>");
            $item.append($("<div class='layui-inline relationalGroup'></div>").append($selectRelational));
            //添加搜索值输入框
            $item.append("<div class='layui-inline'><input class='layui-input' type='text'></div>");
            //添加排序
            $item.append("<a class='searchSorting'><i class='fa fa-arrow-up'></i></a><a class='searchSorting'><i class='fa fa-arrow-down'></i></a>");
            $advancedQueryGroup.append($item);//添加高级查询项
            layui.use("form", function () {
                var form = layui.form;
                form.render(); //刷新渲染表单
            });
        }

        addAdvancedItem();//添加高级查询项

        //生成高级查询对话框
        layui.use(["layer", "element", "form", "laydate"], function () {
            var layer = layui.layer;
            var element = layui.element;
            var form = layui.form;
            var laydate = layui.laydate;
            var index = layer.open({
                title: "高级查询",
                type: 1,
                content: $(dialogId) //这里content是一个普通的String
            });
            layer.style(index, {
                width: '700px',
            });
            form.render(); //刷新渲染表单
            //日期选择器
            laydate.render({
                elem: dialogId + ' .queryItemGroup input.searchItemDate'
                , type: 'date'
            });
            //日期选择器
            laydate.render({
                elem: dialogId + ' .queryItemGroup input.searchItemDatetime'
                , type: 'datetime'
            });
            $(dialogId + " .btnAddQueryItem").click(function () {//高级查询框追加查询项
                addAdvancedItem();
            });
            $(dialogId).on("click", ".searchSorting", function () {//排序事件
                $(this).siblings(".searchSorting").removeClass("selected");//删除同级选中效果
                $(this).addClass("selected");//添加当前选中效果
            });
        });

    }
}